var app = getApp();

const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}


// 显示繁忙提示
var showBusy = text => wx.showToast({
  title: text,
  icon: 'loading',
  duration: 10000
})

// 显示成功提示
var showSuccess = text => wx.showToast({
  title: text,
  icon: 'success'
})

// 显示失败提示
var showModel = (title, content) => {
  wx.hideToast();

  wx.showModal({
    title,
    content: JSON.stringify(content),
    showCancel: false
  })
}

var crtTimeFtt = function(time) {
  var date = new Date(time);

  var year = date.getFullYear(),
    month = date.getMonth() + 1, //月份是从0开始的
    day = date.getDate(),
    hour = date.getHours(),
    min = date.getMinutes(),
    sec = date.getSeconds();
  var newTime = year + '-' +
    month + '-' +
    day + ' ' +
    hour + ':' +
    min + ':' +
    sec;
  return newTime;
}

function productName() {
  return ["H001智能养护内衣", "H003智能胸垫", "H004燃脂内衣"];
}

var getOpenId = function(logidCode, success, fail) {
  wx.request({
    url: 'https://mofeitechnology.com/wap/app-login',
    data: {
      timestamp: "12345678",
      sign: "c50b8afaf0589e296032d2e38aa7f6f1",
      js_code: logidCode
    },
    header: {
      // 这里写接口返回的数据类型,直接帮我们自动解析json xml
      'Content-Type': 'application/json'
    },
    success: function(res) {
      var data = res.data.data.respData;
      var list = JSON.parse(data);
      app.globalData.openId = list.openid;
      app.globalData.sessionKey = list.session_key;
      success(list.openid); // 然后再判断是否有绑定手机号码
    },
    fail: function(error) {
      fail(error);
    }
  })
}

var getPhoneNumber = function(openId, success, failed) {

  /**
   */
  wx.request({
    url: 'https://mofeitechnology.com/wap/get-mobile',
    data: {
      timestamp: "12345678",
      sign: "78bfbec94975433042d22fe5500594aa",
      open_id: openId
    },
    header: {
      // 这里写接口返回的数据类型,直接帮我们自动解析json xml
      'Content-Type': 'application/json'
    },
    success: function(res) {
      var resCode = res.data.code;
      if (resCode == 200) {
        // 跳转页面进行绑定手机号码
        success(res.data.data);
      } else {
        failed(resCode);
      }
    },
    fail: function(error) {
      failed(-1);
    }
  })
}


var getNowTime = function getNowTime() {
  var now = new Date();
  var year = now.getFullYear();
  var month = now.getMonth() + 1;
  var day = now.getDate();
  if (month < 10) {
    month = '0' + month;
  };
  if (day < 10) {
    day = '0' + day;
  };
  //  如果需要时分秒，就放开
  // var h = now.getHours();
  // var m = now.getMinutes();
  // var s = now.getSeconds();
  var formatDate = year + '-' + month + '-' + day;
  return formatDate;
}

var isEmojiCharacter = function(substring) {
  for (var i = 0; i < substring.length; i++) {
    var hs = substring.charCodeAt(i);
    if (0xd800 <= hs && hs <= 0xdbff) {
      if (substring.length > 1) {
        var ls = substring.charCodeAt(i + 1);
        var uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
        if (0x1d000 <= uc && uc <= 0x1f77f) {
          return true;
        }
      }
    } else if (substring.length > 1) {
      var ls = substring.charCodeAt(i + 1);
      if (ls == 0x20e3) {
        return true;
      }
    } else {
      if (0x2100 <= hs && hs <= 0x27ff) {
        return true;
      } else if (0x2B05 <= hs && hs <= 0x2b07) {
        return true;
      } else if (0x2934 <= hs && hs <= 0x2935) {
        return true;
      } else if (0x3297 <= hs && hs <= 0x3299) {
        return true;
      } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 ||
        hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b ||
        hs == 0x2b50) {
        return true;
      }
    }
  }

  return false;
}

module.exports = {
  formatTime,
  showBusy,
  showSuccess,
  showModel,
  crtTimeFtt,
  productName: productName,
  getOpenId,
  getPhoneNumber,
  getNowTime,
  isEmojiCharacter
}